package com.da.web.dorm_room.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.da.common.Result;
import com.da.web.dorm_room.entity.DormRoom;
import com.da.web.dorm_room.entity.RoomListParm;
import com.da.web.dorm_room.entity.RoomParm;
import com.da.web.dorm_room.entity.RoomTree;
import com.da.web.dorm_room.service.DormRoomService;
import com.da.web.dorm_room_bed.entity.DormRoomVo;
import com.da.web.dorm_room_bed.service.DormRoomBedService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @author da
 * @time 2023/12/9 下午 5:09
 */
@RestController
@RequestMapping("/api/room")
public class DormRoomController {
    @Autowired
    private DormRoomService dormRoomService;
    @Autowired
    private DormRoomBedService dormRoomBedService;

    //新增
    @PostMapping
    public Result add(@RequestBody RoomParm parm) {
        boolean b = dormRoomService.addRoom(parm);
        if (b) {
            return Result.success("设置成功");
        }
        return Result.error("设置失败");
    }

    //编辑
    @PutMapping
    public Result edit(@RequestBody DormRoom dormRoom) {
        QueryWrapper<DormRoom> query = new QueryWrapper<>();
        query.lambda().eq(DormRoom::getRoomCode, dormRoom.getRoomCode());
        DormRoom one = dormRoomService.getOne(query);
        if (one != null && !Objects.equals(one.getRoomId(), dormRoom.getRoomId())) {
            return Result.error("编号重复");
        }
        boolean b = dormRoomService.updateById(dormRoom);
        if (b) {
            return Result.success("编辑成功");
        }
        return Result.error("编辑失败");
    }

    @DeleteMapping("/{roomId}")
    public Result delete(@PathVariable("roomId") Long roomId) {
        //需要判断宿舍里面有没有住人,空宿舍才允许删除
        boolean b = dormRoomService.removeById(roomId);
        if (b) {
            return Result.success("删除成功");
        }
        return Result.error("删除失败");
    }

    //列表查询
    @GetMapping("/list")
    public Result getList(RoomListParm parm) {
        IPage<DormRoom> roomPage = new Page<>(parm.getCurrentPage(), parm.getPageSize());
        QueryWrapper<DormRoom> query = new QueryWrapper<>();
        query.lambda().eq(DormRoom::getStoreyId, parm.getStoreyId());
        IPage<DormRoom> page = dormRoomService.page(roomPage, query);
        //查询宿舍对应的床位
        for (DormRoom record : page.getRecords()) {
            //获取宿舍id
            Long roomId = record.getRoomId();
            //查询宿舍对应的床位
            //QueryWrapper<DormRoomBed> queryBed = new QueryWrapper<>();
            //queryBed.lambda().eq(DormRoomBed::getRoomId, roomId);
            List<DormRoomVo> list = dormRoomBedService.getSelectBedList(roomId);
            //设置对应的床位
            record.setBedList(list);
        }
        return Result.success("查询成功", page);
    }

    @GetMapping("/getRoomByStoreyId")
    public Result getRoomByStoreyId(Long storeyId) {
        List<RoomTree> list = dormRoomService.getRoomTree(storeyId);
        return Result.success("查询成功", list);
    }

}
